<template>
	<view class="content">
		<scroll-view scroll-y="true" class="left">
			<view class="type" :class="{active:item.categoryId===activeIndex}" @click="activeIndex=item.categoryId"
				v-for="(item,index) in categoryData" :key="index">
				{{item.categoryName}}
			</view>
		</scroll-view>
		<scroll-view scroll-y="true" class="right">
			<view @click="goGoodsDetail(item.goodsId)" class="goods-box" v-for="(item,index) in goodsCateData" :key="index">
				<image :src="baseUrl+'/'+item.goodsImage" mode="aspectFill"></image>
				<view class="goods-info">
					<view class="goods_name">{{item.goodsName}}</view>
					<view class="goods_price">￥{{item.goodsSellingPrice}}</view>
				</view>
			</view>
		</scroll-view>
	</view>
</template>

<script setup>
	import {
		ref,
		watch
	} from 'vue';
	import {
		get,
		baseUrl
	} from '/hooks/req.js';
	import {
		onLoad
	} from "@dcloudio/uni-app";
	//分类高亮索引
	const activeIndex = ref(0)
	//分类数据
	const categoryData = ref([])
	//分类商品数据
	const goodsCateData = ref([])
	//获取分类数据
	const loadCategory = () => {
		get(`/category/list`).then(result => {
			categoryData.value = result.data.data
			categoryData.value.unshift({
				categoryId: 0,
				categoryName: "全部"
			})
		})
	}
	//获取商品
	const loadGoodsList = (activeIndex) => {
		get(`/goods/category/` + activeIndex).then(result => {
			goodsCateData.value = result.data.data
		})
	}
	//跳转商品详情
	const goGoodsDetail =(goodsId)=>{
		uni.navigateTo({
			url:'/pages/goodsDetail/goodsDetail?goodsId='+goodsId
		})
	}
	watch(activeIndex, (nval) => {
		loadGoodsList(nval)
	})
	onLoad(() => {
		loadCategory()
		loadGoodsList(activeIndex.value)
	})
</script>

<style scoped lang="scss">
	.content {
		display: flex;
		height: 100vh;
		font-size: 14px;
		color: #5e605d;

		.left {
			width: 25%;
			flex-shrink: 0;
			background-color: #fcfcfc;

			.type {
				padding: 20px 0;
				text-align: center;

				&.active {
					background-color: #fff;
					color: #FF6600;
					border-left: 2px solid #FF6600;
				}
			}
		}

		.right {
			.goods-box {
				border-radius: 5px;
				background-color: #F8F8F9;
				height: 80px;
				width: 260px;
				margin: auto;
				display: flex;
				align-items: center;
				margin-top: 5px;
				image {
					margin-left: 20px;
					width: 60px;
					height: 60px;  
				}
				.goods-info {
					margin-left: 20px;
					.goods_name {
						font-size: 14px;
						color: #333;
						margin-right: auto;
						white-space: nowrap;
						overflow: hidden;
						text-overflow: ellipsis;
					}

					.goods_price {
						font-size: 12px;
						color: #FF6600;
						margin-top: 6px;


					}
				}
			}
		}
	}
</style>